<?php
namespace app\admin\model;
use \think\Model;
use User;

class OrderBeans extends Model {
    protected $pk = 'id';
    protected $table = 'sdb_order_beans';
    /*
     * 所有订单数
     */
    public function getAllBeansOrder()
    {
        $AllBeansOrder = $this->order('create_time desc')->select()->toArray();
        return count($AllBeansOrder);
    }
    /*
     * 充值订单数
     */
    public function getRechargeOrder()
    {
        $rechargeOrder = $this->where('user_staff',2)->order('create_time desc')->select()->toArray();
        return count($rechargeOrder);
    }
    /*
     *  提现订单数
     */
    public function getWithdrawOrder()
    {
        $withdrawOrder = $this->where('user_staff',1)->order('create_time desc')->select()->toArray();
        return count($withdrawOrder);
    }
    /*
     * 将所有订单数合并成数组
     */
    public function lists()
    {
        $list['withdraw_num'] = $this->getWithdrawOrder(); //提现订单数
        $list['order_num'] = $this->getAllBeansOrder();    //所有订单数
        $list['recharge_num'] = $this->getRechargeOrder(); //充值订单数
        return $list;
    }
    /**
     * 搜索
     */
    public function getSearchData(User $user,$search)
    {
        if(strlen($search) != 11){
            return $this->where('order_num',$search)->select();
        }
        return $user->where('username',$search)->select();
    }
    /**
     * 联豆用户管理详情（充值联豆总数量）
     * @param $username
     * @return float|int
     */
    public function getRechargeBeans($username)
    {
        return $this->where('username',$username)->where('user_staff',2)->where('order_staff',3)->sum('beans_num');
    }
    /**
     * 联豆用户管理详情（充值总额）
     * @param $username
     * @return float|int
     */
    public function getRechargeMoney($username)
    {
        return $this->where('username',$username)->where('user_staff',2)->where('order_staff',3)->sum('order_money');
    }
    /**
     * 联豆用户管理详情（提现联豆总数）
     * @param $username
     * @return float|int
     */
    public function getWithdrawBeans($username)
    {
        return $this->where('username',$username)->where('user_staff',1)->where('order_staff',3)->sum('beans_num');
    }
    /**
     * 联豆用户管理详情（提现总额）
     * @param $username
     * @return float|int
     */
    public function getWithdrawMoney($username)
    {
        return $this->where('username',$username)
            ->where('user_staff',1)
            ->where('order_staff',3)->sum('order_money');
    }

    /**
     * 联豆用户管理详情（充值订单）
     * @param $username
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getUserRechargeOrders($username)
    {
        return $this->where('username',$username)
            ->where('user_staff',2)
            ->order('create_time desc')->select();
    }

    /**
     * 联豆用户管理详情（提现订单）
     * @param $username
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getUserWithdrawOrders($username)
    {
        return $this->where('username',$username)
            ->where('user_staff',1)
            ->order('create_time desc')->select();
    }

}